package com.hf.providerexport;

import android.Manifest;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hf.providerexport.database.DatabaseHelper;
import com.hf.providerexport.export.FotaProviderExport;
import com.hf.providerexport.export.MediaProviderExport;
import com.hf.providerexport.export.TelephonyProviderExport;
import com.hf.providerexport.export.UriExport;

import java.util.Locale;

public class MainActivity extends PermissionActivity {
    private Handler mHandler = new Handler();

    private TextView mMsgView;
    private ProgressBar mTableProgressView;
    private ProgressBar mProgressView;

    private ProgressUpdateRunnable mProgressUpdateRunnable = new ProgressUpdateRunnable();
    private AlertDialog mProgressDialog = null;

    private UriExport.OnProgressListener mOnProgressListener = new UriExport.OnProgressListener(){
        @Override
        public void onProgress(String msg, int tableCurrent, int tableMax, int current, int max) {
                mHandler.removeCallbacks(mProgressUpdateRunnable);
                mProgressUpdateRunnable.setProgress(msg, tableCurrent, tableMax, current, max);
                mHandler.post(mProgressUpdateRunnable);
        }
    };

    private static String getPath(String path) {
        return String.format(Locale.getDefault(), "%s/ProviderExport/%s",
                Environment.getExternalStorageDirectory(), path);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button exportMediaProviderButton = (Button) findViewById(R.id.export_media_provider);
        exportMediaProviderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        onExportMediaProvider();
                    }
                });
                thread.start();
            }
        });

        Button exportFotaProviderButton = (Button) findViewById(R.id.export_fota_provider);
        exportFotaProviderButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        onExportFotaProvider();
                    }
                });
                thread.start();
            }
        });

        Button exportTelephonyProviderButton = (Button) findViewById(R.id.export_telephony_provider);
        exportTelephonyProviderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        onExportTelephonyProvider();
                    }
                });
                thread.start();
            }
        });
    }

    private void onExportMediaProvider() {
        onExportStarted();

        final String path = getPath("mediaprovider.db");
        DatabaseHelper helper = new DatabaseHelper(path, true);
        MediaProviderExport export = new MediaProviderExport(this);
        export.export(helper, mOnProgressListener);
        helper.close();

        onExportFinished(path);
    }

    private void onExportFotaProvider() {
        onExportStarted();

        final String path = getPath("fotaprovider.db");
        DatabaseHelper helper = new DatabaseHelper(path, true);
        FotaProviderExport export = new FotaProviderExport(this);
        export.export(helper, mOnProgressListener);
        helper.close();

        onExportFinished(path);
    }

    private void onExportTelephonyProvider() {
        onExportStarted();

        final String path = getPath("telephonyprovider.db");
        DatabaseHelper helper = new DatabaseHelper(path, true);
        TelephonyProviderExport export = new TelephonyProviderExport(this);
        export.export(helper, mOnProgressListener);
        helper.close();

        onExportFinished(path);
    }

    private void onExportStarted() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                showDialog();
            }
        });
    }

    private void onExportFinished(final String path) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "Exported to " + path, Toast.LENGTH_SHORT).show();
                dismissDialog();
            }
        });
    }

    private void showDialog() {
        if (mProgressDialog != null) {
            dismissDialog();
        }

        // initialize view
        View dialogView = View.inflate(this, R.layout.progress_dialog, null);
        mMsgView = dialogView.findViewById(R.id.msg);
        mTableProgressView = dialogView.findViewById(R.id.table_progress);
        mProgressView = dialogView.findViewById(R.id.progress);

        // initialize dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
        builder.setTitle(R.string.title_export_progress);

        // create & show dialog
        mProgressDialog = builder.create();
        mProgressDialog.show();
    }

    private void dismissDialog() {
        if (mProgressDialog == null) {
            return;
        }

        mProgressDialog.dismiss();
        mProgressDialog = null;
        mMsgView = null;
        mTableProgressView = null;
        mProgressView = null;
    }

    @Override
    public String[] getPermissionList() {
        return new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
    }

    private class ProgressUpdateRunnable implements Runnable {
        private String mMsg;
        private int mTableCurrent;
        private int mTableMax;
        private int mCurrent;
        private int mMax;

        void setProgress(String msg, int tableCurrent, int tableMax, int current, int max) {
            mMsg = msg;
            mTableCurrent = tableCurrent;
            mTableMax = tableMax;
            mCurrent = current;
            mMax = max;
        }

        @Override
        public void run() {
            if (mProgressDialog != null) {
                mMsgView.setText(mMsg);
                if (mTableProgressView.getMax() != mTableMax) {
                    mTableProgressView.setMax(mTableMax);
                }
                mTableProgressView.setProgress(mTableCurrent);
                if (mProgressView.getMax() != mMax) {
                    mProgressView.setMax(mMax);
                }
                mProgressView.setProgress(mCurrent);
            }
        }
    }
}
